package com.ssl.tensquare.recruit.controller;

import com.ssl.tensquare.common.entity.Result;
import com.ssl.tensquare.common.entity.StatusCode;
import com.ssl.tensquare.common.util.WebUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.MessageFormat;

/**
 * 描述：
 *
 * @author ssl
 * @create 2019/06/10 14:50
 */
@ControllerAdvice
public class RecruitExceptionHandler {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @ExceptionHandler(value = {Exception.class})
    @ResponseBody
    public Result exception(HttpServletRequest request, HttpServletResponse response, Exception e) {
        String errorMsg = e.getMessage();
        String detailMsg = MessageFormat.format("ip:[{0}],request:{1},error:{2}", WebUtil.getClientIP(request),
                WebUtil.getParameters(request), errorMsg);
        logger.error(detailMsg, e);
        return new Result(false, StatusCode.ERROR, errorMsg);
    }
}
